<?php 
//echo $_GET['date'];
date_default_timezone_set('America/Los_Angeles');
include "../includes/functions.php";  
//echo $_GET['date'];
if (date ('H') > '03') {
	$date = substr($_GET['date'],0,10);
}else{
 $date = substr($_GET['date'],0,5) . substr($_GET['date'],5,3) . (padWithZeros(substr($_GET['date'],8,3)- 1,2) ); // show yesterday if before 3am
 $wee_hours = 1;
} 

//echo substr($_GET['date'],8,2) . "||". date("d");
//echo ">>".$date;


//$adjusted_time = mktime(date("h"), date("i"), date("s"), date("m"), date("d"),   date("Y"));
$time_now = date ('H:i:s'); //echo $time_now;
//playlistsecho "timenow: $time_now  ";


echo db_connect();

$display_date = strtotime($date);
$display_date = date("l F j, Y",$display_date);

if ( (substr($_GET['date'],8,2) == date("d")) || (substr($_GET['date'],8,2) == (date("d") -1) && $wee_hours == 1 ) )  {  //we're looking at today
   $today_flag = 1;
	$display_date .= " (today)"; 
  $current_time = date ('g:i a');
} 

if ($today_flag) {
	$change_day_butt = "<span class='change_day_butt'> <a href='playlists_date_pick.php' style='color:blue;'>» Different Day</a></span>";
} else {
	$change_day_butt = "<span class='change_day_butt'><a href='playlists.php' style='color:red;font-weight:bold;'>»Today</a></span> <span class='change_day_butt'> <a href='playlists_date_pick.php' style='color:blue;'>» Different Day</a></span>";  
}

$header = "<div class='date_header'>$display_date $change_day_butt<div class='time'>$current_time</div></div>";

$query='SELECT * FROM show_events WHERE `date` LIKE "'.$date.'%"  ORDER BY start_time';   
 //echo $query;
if (!$result= mysql_query($query)) echo mysql_error();
   
 while ($row= mysql_fetch_array($result)) {
	   $xx = 1; 
 		$show_event_id=$row['id'];
		$show_id=$row['show_id'];  
		$show_occurrance_id=$row['show_occurrance_id'];
		$show_name=$row['show_name']; 

		$start_time= substr($row['start_time'],0,5);
		$start_time_display = timeFormat($start_time);
		$end_time= $row['end_time']; 
		// put selected if it's today, now logic here 
		if (($time_now < $end_time && $time_now > $start_time) && $today_flag) {$class="event_row_current";  }else{$class="event_row"; }
		
		/// get schedules/schedules done
		$sc=0; $num_done=0;
		$sdq = "SELECT id FROM sponsorship_schedules WHERE show_id ='$show_occurrance_id' AND (datetime = '$date' OR datetime = '0000-00-00 00:00:00')"; 	
		$schedules_result = mysql_query($sdq);
		while($schedules_row = mysql_fetch_array($schedules_result)) {
			$sc_id = $schedules_row[0];
			$sdrq = "SELECT COUNT(*) FROM schedules_done WHERE schedule_id = '$sc_id'"; $sch_done_result = mysql_query($sdrq);
			$shc_done_row = mysql_fetch_array($sch_done_result); if ($shc_done_row[0]) $num_done ++;  
			$sc++; 
			}
         if ($num_done < $sc) {  $num_done = "<strong><span style=' padding:1px;background-color:orange;'>$num_done</span></strong>";}

		
		$sql2 = 'SELECT * from playlists where show_occurrance_id = "'.$show_occurrance_id.'" AND datetime LIKE "'.$date.'%" LIMIT 1';
		$result2 = mysql_query($sql2);
		$row2 = mysql_fetch_array($result2);
		if ($row2[0]) { // playlist created
			$playlist_id = $row2[0];
			
			$item_query = 'select playlist_item_id from playlist_items where playlist_id = "'.$playlist_id.'"';
			$item_result= mysql_query($item_query);
			$e=0;
			
			//echo "||$item_query";
			
			while ($item_row= mysql_fetch_array($item_result))  {
				$item = $item_row[0];
				$e++;
			}
			$tracks_count = " [$e tracks]";
			 
					if ($e) { // playlist has tracks created
						$playlist_bit = "<a href='playlist_new.php?show_id=$show_id&playlist_id=$playlist_id'><span style='color:#167700;font-weight:bold; font-size:12px;'>» Playlist</span>$tracks_count<span style='position:absolute;right:20px;'>Spots: $num_done/$sc</span></a>"; 
					} else { // we dress up the link to look like we're creating a pl
				  		$playlist_bit = "<span style='color:#e77102;font-weight:bold;'>No Playlist</span><a href='playlist_new.php?show_id=$show_id&playlist_id=$playlist_id'>  »Create Playlist <span style='position:absolute;right:20px;'>Spots: $num_done/$sc</span></a>";  
					}
			
			
			} else { // no PL created
				
				 $playlist_id = "";
				$playlist_bit = "<form id='form$show_event_id' name='form$show_event_id' method='post' action='playlist_new.php'><input type='hidden' name='new_date' value='$date'><input type='hidden' name='show_id' value='$show_id'></form><span style='color:#e77102;font-weight:bold;'>No Playlist</span><a href='#' onClick='document.form$show_event_id.submit();'>  »Create Playlist <span style='position:absolute;right:20px;'>Spots: $num_done/$sc</span></a>"; 
				
				}
		
		$rows .= "<div id='s-$show_event_id' class='$class'>
			<table width='100%'><tr>
			<td width='300'><div style= 'min-width:300px;font-size:12px;'><strong>$show_name</strong> <span style='font-size:9px'> $start_time_display</span></div></td>
			<td><span class='playlist_bit'>$playlist_bit</span></td>
			</tr>
			</table>
		</div>\n"; 
		//$row_html .= "<div id='s-$show_event_id' class='event_row'>$show_name<span class='playlist_bit'></span></div>\n";

}

if (!$xx) { // playlists didn't get created; create them now
    
  // turned this off because it was creating duplicates. we'll rely on the cron for now  04-10
  // header ("location:http://www.cjly.ca/studio/admin/_cron_save_show_events.php");
  // echo "Loading, please wait...";

} else {
  echo "$header<br>$rows";	
}
 ?>

